stats_mod = {
    urlSend: 'stats.php?id=$id',
    urlRecive: 'getstats.php',
    id: 'plus_stats',
    idResponse: 'plus_stats_response',
    html: '<div style="margin-top: 7px"><h2>Estadisticas</h2><div class="body"><img src="[url_stas_img]" style="border:none;magin-top:15px"/><div style="text-align: center"><b><span style="color: green">online</span></b>: [users_online] / <b><span style="color: red">offline</span></b>: [users_offline]</div></div></div>',
    chart: 'http://chart.apis.google.com/chart?chs=171x50&cht=lfi&chco=0077CC&chm=B,E6F2FA,0,0,0&chls=1,0,0&chf=bg,s,ffffff&chd=t:[stats_values]',
    init: function() {
        config_mod.onsaveconfig.add(this);
        config_mod.onload.add(this);
        system_lib.onmessage.add(this);

        var cfg = tuenti_lib.config.get();
        this.setStatsCounter(cfg.stats);
    },
    setStatsCounter: function (enabled) {
        var stel = document.getElementById(this.id);

        if ((stel && enabled) || (!stel && !enabled) ) {
            return;
        }

        if (enabled) {
            var header = document.getElementById('header');
            var data = JSON.decode(/\{.*?\}/.exec(document.getElementById('tab_logout').onclick)[0]);
            var img = document.createElement('img');
            img.src = config_plus.statsUrl + this.urlSend.replace(/\$id/, system_lib.md5('' + data.user_id));
            img.className = 'hide';
            img.id = this.id;

            header.appendChild(img);
        }
        else {
            stel.parentNode.removeChild(stel);
        }
    },
    onSaveConfig: function(cfg) {
        this.setStatsCounter(cfg.stats);
    },
    //Aprovechamos el registro para optener en JSON las stats actuales
    //stats: { chart: [1,2,3,...], total: 50000 }
    onResponse: function(stats) {
        if (!stats) return;

        var rightbar = system_lib.select('#settings div.sidebar.right')[0] || undefined;
        if (!rightbar) {
            console.error('stats: no se encuentra la barra lateral');
            return;
        }

        var block = document.createElement('div');
        block.className = 'mod';

        var chart = this.chart.replace('[stats_values]', stats.chart.join(','));

        var html = this.html.replace('[url_stas_img]', chart);
        html = html.replace('[users_online]', stats.online);
        html = html.replace('[users_offline]', stats.total - stats.online);

        block.innerHTML = html;

        //añadimos como primer bloque de todos
        if (rightbar.firstChild)
            rightbar.insertBefore(block, rightbar.firstChild);
        else
            rightbar.appendChild(block);

        document.getElementsByTagName('head')[0].removeChild(document.getElementById(this.idResponse));
    },
    onLoadConfig: function (cfg) {
        if (cfg.stats) {
            var iframe = document.createElement('iframe');
            iframe.src = config_plus.statsUrl + this.urlRecive;
            iframe.id = this.idResponse;
            document.getElementsByTagName('head')[0].appendChild(iframe);
        }
    },
    onMessage: function (event) {

        if (/(^https?:\/\/[0-9a-z.\-]+)/i.exec(config_plus.statsUrl)[1] != event.origin)
            return;

        var data = JSON.decode(event.data);

        if (data.type != 'stats')
            return;

        this.onResponse(data);
    }
};

stats_mod.init();